﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using bdxyit.com.RecruitmentAppSys.IDAL;
using bdxyit.com.RecruitmentAppSys.Models;
using System.Data;
using bdxyit.com.RecruitmentAppSys.DBUtility;
using System.Data.SqlClient;

/***
*机器名：BDXYIT-HUI
*项目名称：$projectname$
*命名空间：bdxyit.com.RecruitmentAppSys.SQLDAL
*文件名：EducationListModels
*系统时间：3/25/2012 3:34:49 PM
*当前用户：huihui
*创建时间：2012
*
*修改时间：2012 3/25/2012 3:34:49 PM
*修改事件：
***/

namespace bdxyit.com.RecruitmentAppSys.SQLDAL
{
    public class EducationListDAL : IEducationListDAL
    {
        private const string SQL_SELECT_EDUCATIONLIST = " select EduID,EduName from EducationLists ";
        private const string SQL_SELECT_EDUCATIONLIST_BY_EDUID = " select  top 1 EduID,EduName from EducationLists "
                                                                + "  where EduID=@EduID";
        private const string SQL_DELETE_EDUCATIONLIST_BY_EDUID = " delete from EducationLists "
                                                              + "  where EduID=@EduID";
        private const string SQL_UPDATE_EDUCATIONLIST_BY_EDUID = " update EducationLists set "
                                                                + " EduID=@EduID,"
                                                                + " EduName=@EduName"
                                                                + "  where EduID=@EduID";
        private const string SQL_INSERT_EDUCATIONLIST = " insert into EducationLists("
                                                    + " EduID,EduName)"
                                                    + "  values ("
                                                    + " @EduID,@EduName)";
        private const string FIELD_EduID = "@EduID";
        private const string FIELD_EduName = "@EduName";
        public bool InsertEducationList(EducationList cmodel)
        {
            bool flag = false;
            SqlParameter[] EducationListParms = GetEducationListParameters();
            SqlCommand cmd = new SqlCommand();
            //写添赋值语句
            EducationListParms[0].Value = cmodel.EduID;
            EducationListParms[1].Value = cmodel.EduName;

            foreach (SqlParameter parm in EducationListParms)
                cmd.Parameters.Add(parm);
            using (SqlConnection conn = new SqlConnection(SqlHelper.ConnectionStringLocal))
            {
                conn.Open();
                cmd.Connection = conn;
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = SQL_INSERT_EDUCATIONLIST;
                cmd.ExecuteNonQuery();
                cmd.Parameters.Clear();
                flag = true;
            }
            return flag;
        }
        public bool DeleteEducationList(string EduID)
        {
            bool flag = false;
            //创建参数
            SqlParameter parm = new SqlParameter(FIELD_EduID, SqlDbType.VarChar, 10);
            parm.Value = EduID;
            SqlCommand cmd = new SqlCommand();
            using (SqlConnection conn = new SqlConnection(SqlHelper.ConnectionStringLocal))
            {
                conn.Open();
                cmd.Connection = conn;
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = SQL_DELETE_EDUCATIONLIST_BY_EDUID;
                cmd.Parameters.Add(parm);
                cmd.ExecuteNonQuery();
                cmd.Parameters.Clear();
                flag = true;
            }
            return flag;
        }
        public bool UpdateEducationList(EducationList cmodel)
        {
            bool flag = false;
            SqlParameter[] EducationListParms = GetEducationListParameters();
            SqlCommand cmd = new SqlCommand();
            //写添赋值语句
            EducationListParms[0].Value = cmodel.EduID;
            EducationListParms[1].Value = cmodel.EduName;

            foreach (SqlParameter parm in EducationListParms)
                cmd.Parameters.Add(parm);
            using (SqlConnection conn = new SqlConnection(SqlHelper.ConnectionStringLocal))
            {
                conn.Open();
                cmd.Connection = conn;
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = SQL_UPDATE_EDUCATIONLIST_BY_EDUID;
                cmd.ExecuteNonQuery();
                cmd.Parameters.Clear();
                flag = true;
            }
            return flag;
        }
        public EducationList SelectEducationListByEduID(string EduID)
        {
            EducationList cmodel = null;
            //创建参数
            SqlParameter parm = new SqlParameter(FIELD_EduID, SqlDbType.VarChar, 10);
            parm.Value = EduID;
            using (SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocal, CommandType.Text, SQL_SELECT_EDUCATIONLIST_BY_EDUID, parm))
            {
                if (dr.Read())
                {
                    cmodel = new EducationList();
                    if (dr.HasRows == true)
                    {
                        if (dr["EduID"] != null && dr["EduID"].ToString() != "")
                        {
                            cmodel.EduID = dr["EduID"].ToString();
                        }
                        if (dr["EduName"] != null && dr["EduName"].ToString() != "")
                        {
                            cmodel.EduName = dr["EduName"].ToString();
                        }
                    }
                }
            }
            return cmodel;
        }
        public List<EducationList> SelectEducationList()
        {
            List<EducationList> selectlist = new List<EducationList>();
            using (SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocal, CommandType.Text, SQL_SELECT_EDUCATIONLIST))
            {
                while (dr.Read())
                {
                    EducationList cmodel = new EducationList();
                    //写添赋值语句
                    if (dr["EduID"] != null && dr["EduID"].ToString() != "")
                    {
                        cmodel.EduID = dr["EduID"].ToString();
                    }
                    if (dr["EduName"] != null && dr["EduName"].ToString() != "")
                    {
                        cmodel.EduName = dr["EduName"].ToString();
                    }

                    selectlist.Add(cmodel);
                }
            }
            return selectlist;
        }
        private SqlParameter[] GetEducationListParameters()
        {
            SqlParameter[] parms = new SqlParameter[] {
                	new SqlParameter(FIELD_EduID, SqlDbType.VarChar,10),
					new SqlParameter(FIELD_EduName, SqlDbType.VarChar,50)};
            return parms;
        }

    }
}